package Phase1;

import Phase1.RegexParser.RegexFileParser;

import java.io.FileReader;
import java.io.Reader;

/**
 * Created with IntelliJ IDEA.
 * User: Alvin
 * Date: 11/10/12
 * Time: 5:09 PM
 * To change this template use File | Settings | File Templates.
 */
public class ScanGenTester {
    public static void main(String[] args) {
        try {
            //String regexfilename = args[0];
            //String inputfilename = args[1];
            //RegexFileParser p = new RegexFileParser(regexfilename);
            //Reader reader = new FileReader(inputfilename);
            RegexFileParser p = new RegexFileParser("spec");
            Reader reader = new FileReader("input");
            String previousMatch = "";
            String currentMatch = "";
            boolean tokenEnded = false;
            String token = "";
            int r;
            r = reader.read();
            token += (char)r;
            currentMatch = p.MatchToken(token);
            previousMatch = currentMatch;
            while(true) {
                if(tokenEnded) {
                    token = token.substring(0,token.length()-1);
                    System.out.println(previousMatch.substring(1) + " " + token);
                    token = (char)r+"";
                    tokenEnded = false;
                    previousMatch = currentMatch;
                    currentMatch = p.MatchToken(token);
                }
                else {
                    r = reader.read();
                    if(r == -1) {
                        currentMatch = p.MatchToken(token);
                        if(currentMatch != null) {
                            System.out.println(currentMatch.substring(1) + " " + token);
                        }
                        break;
                    }
                    if(((char)r+"").trim().length()>0) {
                        token += (char)r;
                        previousMatch = currentMatch;
                        currentMatch = p.MatchToken(token);
                        if(currentMatch != previousMatch && currentMatch == null) {
                            if(previousMatch != null) {
                                tokenEnded = true;
                                if(currentMatch == null) {
                                    currentMatch = previousMatch;
                                }
                            }
                            else {
                                previousMatch = currentMatch;
                            }
                        }
                    }
                }
            }

            String temp = p.MatchToken("a");
            int i = 1;
        }
        catch (Exception e) {
            System.out.println("Shit's jank");
            System.out.println(e.getMessage());
        }
    }
}
